$(function($) {
	loadedSize = 1;
	load_videos = function(){
		$('#videosContent').scrollPagination({
			'contentPage' : "loadVideo", // the url you are fetching the results
			//'contentData' : {}, // these are the variables you can pass to the request, for example: children().size() to know which page you are
			'scrollTarget' : $(window), // who gonna scroll? in this example, the full window
			'heightOffset' : 400, // it gonna request when scroll is 10 pixels before the page ends
			'beforeLoad' : function() { // before load function, you can display a preloader div
				$('#loader').fadeIn();
				if (loadedSize != $('#videosContent').children().size()) {
					loadedSize = $('#videosContent').children().size();
					$(this)[0].contentData = {
						loadedSize : loadedSize
					};
				} else {
					$(this)[0].contentData = {
						loadedSize : -1
					};
				}

			},
			'afterLoad' : function(elementsLoaded) { // after loading content, you can use this function to animate your new elements
				$('#loader').fadeOut();
				if(typeof FB != 'undefined'){
					FB.XFBML.parse();
				}
			}
		});
	};
	
	load_hot_videos = function(){
		$.get("/hot-video", function(data) {
			for (var i = 0; i < data.length; ++i) {
				var obj = data[i];
				var html = "<div class='videoListItemSmall'>"
				+ "<a href='/video/" + obj.id + "'>"
				+ "<div class='thumb'> <img src='http://i.ytimg.com/vi/"+ obj.youtubeId + "/" + obj.smallThumbnail +"'><div class='duration'>"+ obj.duration +"</div></div>"
				+ "<div class='info'> <h3 class='title'>" + obj.title + "</h3>"
				+ "<div class='stats'> <span class='views-small' title='lượt xem'>" + obj.viewNumber.format()
				+ "</span> <span class='comments-small' title='lượt bình luận'>" + obj.commentNumber.format()
				+ "</span></div></div><div class='clear'></div> </a> </div>";
				$("#hot-video").append(html);
			};
		}, 'json').fail(function(e, data) {
			alert("error " + data);
		});
	};
	
	Number.prototype.format = function(n, x) {
	    var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')';
	    return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&.');
	};
});
